/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import play.db.jpa.Model;

/**
 * 
 * @author Ousama
 */
@Entity
public class Comment extends Model {

    @ManyToOne
    public IdeaDeveloper creator;
    @ManyToOne
    public Idea idea = null;
    @ManyToMany(mappedBy = "spamComment", cascade = CascadeType.PERSIST)
    public List<IdeaDeveloper> spam = new ArrayList<IdeaDeveloper>();
    @ManyToOne
    public ActionPlan actionPlan = null;
    public String body;
    @ManyToOne
    public Timeline timeline = null;
    public Date date;

    /**
     * Creates a new comment on an Idea
     *
     * @author AbdelRahman Zein
     * @param dev
     *            the idea developer writing the comment
     * @param idea
     *            the idea where the idea developer commenting on
     * @param body
     *            the text of the comment
     * @return the posted comment
     */
    public static Comment create(IdeaDeveloper dev, Idea idea, String body) {
        Comment c = new Comment();
        c.body = body;
        c.creator = dev;
        c.idea = idea;
        c.save();
        dev.createdComments.add(c);
        dev.numberOfComments ++;
        dev.save();
        idea.comments.add(c);
        idea.save();

        Notification.notify(" has just commented on idea <a href=\"http://localhost:9000/topicideacontroller/checkauthorizationtoviewidea?ideaId="+ idea.id + "\">"+ idea.title + "</a>", idea.creator,dev,  new Date());

        Log log = new Log("^ commented on ^.");
        log.ids = dev.id + "," + idea.id;
        log.types = "developer,idea";
        log.save();

        return c;
    }

    /**
     *
     * Creates a new comment on an action plan
     *
     * @author AbdelRahman Zein
     * @param dev
     *            the idea developer writing the comment
     * @param plan
     *            the plan where the idea developer commenting on
     * @param body
     *            the text of the comment
     * @return the posted comment
     */
    public static Comment create(IdeaDeveloper dev, ActionPlan plan, String body) {
        Comment c = new Comment();
        c.body = body;
        c.creator = dev;
        c.actionPlan = plan;
        c.save();
        plan.comments.add(c);
        plan.save();
        dev.createdComments.add(c);
        dev.numberOfComments ++;
        dev.save();

        for (int i = 0; i < plan.managers.size(); i++) {
            Notification.notify("<p><a href=\"http://localhost:9000/actionplancontroller/actionplanorganizer?actionPlanId="+ plan.id + "\">" + dev.username + " just commented on action plan of name: "+ plan.name + "</a></p>",  plan.managers.get(i).ideaDeveloper, dev,new Date());

        }

        Log log = new Log("^ commented on ^.");
        log.ids = dev.id + "," + plan.id;
        log.types = "developer,actionplan";
        log.save();

        return c;
    }   
   
    /**
     * Helper to delete the comment and all its components
     *
     * @author AbdelRahman Zein
     */
    public void delete2() {
        // delete it from the creator
        this.creator.createdComments.remove(this);
        this.creator.save();
        // deleting from the spam done by developers
        for (int i = 0; i < spam.size(); i++) {
            spam.get(i).spamComment.remove(this);
            spam.get(i).save();
        }

        // deleting it from the comments in his idea
        if (idea != null) {
            idea.comments.remove(this);
            idea.save();
        } else if (actionPlan != null) {
            actionPlan.comments.remove(this);
            actionPlan.save();
        }
        

        Log log = new Log("^ deleted his comment.");
        log.ids = this.creator.id + "";
        log.types = "developer";
        log.save();
        this.delete();

    }

    /**
     * to make user able to report any comment as spam
     *
     * @author Amira
     * @param ideaDeveloperId
     *            the idea developer want to report comment as spam
     */
    public void reportAsSpam(long ideaDeveloperId) {

        IdeaDeveloper ideaDeveloper = (IdeaDeveloper) IdeaDeveloper.find("id=?", ideaDeveloperId).fetch().get(0);
        this.spam.add(ideaDeveloper);
        this.save();

        Log log = new Log("^ reported ^ as spam.");
        log.ids = ideaDeveloperId + "," + this.id;
        log.types = "developer,comment";
        log.save();


        ideaDeveloper.spamComment.add(this);
        ideaDeveloper.save();
        int x;
        if (this.idea != null) {
            x = this.idea.comments.indexOf(this);
            String description = " has reported comment no " + x + " in this idea : <p><a href=\"http://localhost:9000/topicideacontroller/checkauthorizationtoviewidea?ideaId="
                    + idea.id + "\">" + idea.title + "</a></p>";
            if (idea.ideaEnt != null) {
                for (int i = 0; i < idea.ideaEnt.managers.size(); i++) {
                    Notification.notify(description, idea.ideaEnt.managers.get(i).ideaDeveloper, ideaDeveloper, new Date());
                }
            }
            if (idea.ideaOrg != null) {
                for (int i = 0; i < idea.ideaOrg.managers.size(); i++) {
                    Notification.notify(description, idea.ideaOrg.managers.get(i).ideaDeveloper, ideaDeveloper, new Date());
                }
            }
            if (idea.ideaTopic != null) {
                if (idea.ideaTopic.topicEnt != null) {
                    for (int i = 0; i < idea.ideaTopic.topicEnt.managers.size(); i++) {
                        Notification.notify(description, idea.ideaTopic.topicEnt.managers.get(i).ideaDeveloper, ideaDeveloper, new Date());
                    }
                }
                if (idea.ideaTopic.topicOrg != null) {
                    for (int i = 0; i < idea.ideaTopic.topicOrg.managers.size(); i++) {
                        Notification.notify(description, idea.ideaTopic.topicOrg.managers.get(i).ideaDeveloper, ideaDeveloper, new Date());
                    }
                }
            }

        } else {
            x = this.actionPlan.comments.indexOf(this);
            String description = " has reported comment no " + x + " in Action Plan" + this.actionPlan.name;

            for (int i = 0; i < this.actionPlan.managers.size(); i++) {
                Notification.notify(description, actionPlan.managers.get(i).ideaDeveloper, ideaDeveloper, new Date());
            }

        }

    }
}
